package com.solarStruggle.graphics

import java.awt.{Color, Point}
import util.Random

object StarrySkyGenerator {
  def getNebulaImage(size: Int): BufferedImage = {
    val r = new Random()
  
    def createPoint(): Point =
      new Point(r.nextRangedFloat(size * 0.2f, size * 0.8f).toInt, r.nextRangedFloat(size * 0.2f, size * 0.8f).toInt)
    
    def createPoints(n: Int): List[Point] =
      n match {
        case 0 => List()
        case _ => createPoint() :: createPoints(n - 1)
      }
    
    val seedImage = new MetaBallGenerator(size, size, new Color(0, 0, 0, 0), 32, 1, 50, createPoints(50)) {
      def colorFunction(value: Float): Color =
        ColorSpec(new Color(0.3f * value, 0.01f * value,  0.8f * value, 0.75f), 
                  (-0.75f, -0.25f), ColorSpec.VaryBy(0.2f), ColorSpec.Invariant).getColor(r)
    }.createImage()
  
    new DiamondSquare(32, ColorSpec(Color.WHITE, ColorSpec.VaryBy(0.005f), ColorSpec.Invariant, ColorSpec.Invariant), seedImage).createImage()
  }
}

class StarrySkyGenerator(size: Int)
extends LayeredImageGenerator(
  List(new StarField(size, size, 2, Color.BLACK,
         RandomColorDescriptor(ColorSpec(Color.WHITE, (-0.75f, -0.25f), (0.0f, 0.0f), (0.0f, 0.0f)),
         (0.1f, ColorSpec(Color.RED, (-0.8f, -0.4f), (0.0f, 0.0f), (0.0f, 0.0f))))).createImage(),
       StarrySkyGenerator.getNebulaImage(size),
       new StarField(size, size, 1, new Color(0, 0, 0, 0),
        RandomColorDescriptor(ColorSpec(Color.WHITE, (-0.75f, 0.0f), (0.0f, 0.0f), (0.0f, 0.0f)))).createImage()))
